# utils/adb_utils.py

import subprocess


def run_adb(cmd):
    result = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return result.stdout.decode().strip()


def get_device_serials():
    output = run_adb("adb devices")
    return [line.split()[0] for line in output.splitlines() if "\tdevice" in line]


def install_app(serial, apk_path):
    run_adb(f"adb -s {serial} install {apk_path}")


def get_logcat(serial, tag=None):
    cmd = f"adb -s {serial} logcat -d"
    if tag:
        cmd += f" *:S {tag}:V"
    return run_adb(cmd)



def set_device_language(serial, lang="en"):
    cmd_map = {
        "en": "setprop persist.sys.locale en-US",
        "zh": "setprop persist.sys.locale zh-CN",
        "fr": "setprop persist.sys.locale fr-FR",
        # 可扩展更多语言
    }
    if lang not in cmd_map:
        raise ValueError(f"不支持的语言: {lang}")
    subprocess.run(f"adb -s {serial} shell {cmd_map[lang]}", shell=True)
    subprocess.run(f"adb -s {serial} reboot", shell=True)
    time.sleep(10)  # 等待重启完成









